package com.isdepci.project.carbon.controller;

import com.isdepci.common.utils.poi.ExcelUtil;
import com.isdepci.framework.aspectj.lang.annotation.Log;
import com.isdepci.framework.aspectj.lang.enums.BusinessType;
import com.isdepci.framework.web.controller.BaseController;
import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.framework.web.page.TableDataInfo;
import com.isdepci.project.carbon.domain.BCcerTrade;
import com.isdepci.project.carbon.domain.param.WeatherDataExcel;
import com.isdepci.project.carbon.service.IBCcerTradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 碳交易Controller
 *
 * @author isdepci
 * @date 2025-03-29
 */
@RestController
@RequestMapping("/ccer/trade")
public class BCcerTradeController extends BaseController
{
    @Autowired
    private IBCcerTradeService BCcerTradeService;


    /**
     * 根据项目ID查询并计算CCER和碳普惠交易量总和
     *
     * @param projectId 项目ID
     * @return 返回CCER和碳普惠交易量的总和
     */
    @GetMapping("/calculateCarbonVolume")
    public AjaxResult calculateCarbonVolume(@RequestParam Long projectId) {
        return BCcerTradeService.calculateTotalCarbonVolume(projectId);
    }




    /**
     * 查询碳交易列表
     */
//   //  @PreAuthorize("@ss.hasPermi('carbon:trade:list')")
    @GetMapping("/list")
    public TableDataInfo list(BCcerTrade bCcerTrade)
    {
        startPage();
        List<BCcerTrade> list = BCcerTradeService.selectBCcerTradeList(bCcerTrade);
        return getDataTable(list);
    }

    /**
     * 导出碳交易列表
     */
//   //  @PreAuthorize("@ss.hasPermi('carbon:trade:export')")
    @Log(title = "碳交易", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BCcerTrade bCcerTrade)
    {
        List<BCcerTrade> list = BCcerTradeService.selectBCcerTradeList(bCcerTrade);
        ExcelUtil<BCcerTrade> util = new ExcelUtil<BCcerTrade>(BCcerTrade.class);
        return util.exportExcel(list, "trade");
    }


    /**
     * 上传保存数据
     * @param excel
     * @return
     */
    @PostMapping("/changeData")
    public AjaxResult changeDataByProjectId(@RequestBody WeatherDataExcel excel) {
        return BCcerTradeService.changeDataByProjectId(excel);
    }


    /**
     * 获取碳交易详细信息
     */
   //  @PreAuthorize("@ss.hasPermi('carbon:trade:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(BCcerTradeService.selectBCcerTradeById(id));
    }

    /**
     * 新增碳交易
     */
   //  @PreAuthorize("@ss.hasPermi('carbon:trade:add')")
    @Log(title = "碳交易", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BCcerTrade bCcerTrade)
    {
        return toAjax(BCcerTradeService.insertBCcerTrade(bCcerTrade));
    }

    /**
     * 修改碳交易
     */
   //  @PreAuthorize("@ss.hasPermi('carbon:trade:edit')")
    @Log(title = "碳交易", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BCcerTrade bCcerTrade)
    {
        return toAjax(BCcerTradeService.updateBCcerTrade(bCcerTrade));
    }

    /**
     * 删除碳交易
     */
   //  @PreAuthorize("@ss.hasPermi('carbon:trade:remove')")
    @Log(title = "碳交易", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(BCcerTradeService.deleteBCcerTradeByIds(ids));
    }
}
